﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.Base;
using UFIDA.U9.Base.Currency;
using UFIDA.U9.Base.Organization;
using UFIDA.U9.CBO.FI.BankCardType;
using UFIDA.U9.CBO.FI.ExpenseItem;
using UFIDA.U9.CBO.HR.Department;
using UFIDA.U9.CBO.HR.Operator;
using UFIDA.U9.CBO.HR.Person;
using UFIDA.U9.CBO.SCM.ProjectTask;
using UFIDA.U9.ER.Enums;
using UFIDA.U9.ER.Loan;
using UFIDA.U9.ER.LoanBill;
using UFIDA.U9.ER.LoanBillBP;
using UFIDA.U9.ER.LoanBillBP.Proxy;
using UFIDA.U9.ISV.ERMolibeISV;
using UFSoft.UBF.Business;
using UFSoft.UBF.Transactions;

namespace ufida.u9.ait.openapi
{
    internal class LoanBillBase
    {
        public static LoanBill DeleteDoc(LoanBill doc)
        {
            if (doc == null) return null;
            U9Utils.ClearCache(doc.Key);
            doc = doc.Key.GetEntity();

            if (doc.DocStatus == LoanDocStatusEnum.Approved)
            {
                //弃审
                doc=UnApproveDoc(doc);
            }

            //删除
            using(ISession session = Session.Open())
            {
                doc.Remove();
                session.Commit();
            }
            return doc;
        }
        public static LoanBill CreateDoc(LoanBillAITDTOData dto)
        {
            LoanBill doc = null;
            using (ISession session = Session.Open())
            {
                doc = LoanBill.Create();
                doc.DocNo = "自动编号";
                if (dto.LoanBillType == null) throw new Exception("LoanBillType必填");
                doc.LoanBillType = EntityUtils.GetEntityByCode<LoanDocType>(dto.LoanBillType.Code);
                if (string.IsNullOrEmpty(dto.LoanDate)) throw new Exception("LoanDate必填");
                doc.LoanDate = DateTime.Parse(dto.LoanDate);
                doc.BusinessDate = doc.LoanDate;
                doc.AccountPeriod = EntityUtils.getPostPeriod(doc.LoanDate);
                if (!string.IsNullOrEmpty(dto.ExpectPayDate))
                {
                    doc.ExpectPayDate = DateTime.Parse(dto.ExpectPayDate);
                }
                //组织
                doc.Org = Context.LoginOrg;
                doc.LegacyOrg = doc.Org;
                doc.AuditOrg = doc.Org;
                //借款组织
                doc.LoanOrg = doc.Org;
                if (dto.LoanOrg != null)
                {
                    doc.LoanOrg = EntityUtils.GetEntityByCode<Organization>(dto.LoanOrg.Code);
                }

                //借款人组织
                //doc.LoanPersonOrg = doc.Org;
                //if (dto.LoanPersonOrg != null)
                //{
                //    doc.LoanPersonOrg = EntityUtils.GetEntityByCode<Organization>(dto.LoanPersonOrg.Code);
                //}

                //借款人
                if (dto.LoanPerson == null) throw new Exception("LoanPerson必填");
                doc.LoanPerson = EntityUtils.GetEntityByPath<EmployeeArchive>($"EmployeeCode='{dto.LoanPerson.Code}'");
                //借款人组织
                //依据借款人获取借款人组织
                doc.LoanPersonOrg=EntityUtils.GetEmployeeOrg(doc.LoanPerson);
                if (doc.LoanPersonOrg == null) throw new Exception($"借款人[{dto.LoanPerson.Code}]不存在主任职业务组织");

                //金额
                doc.LoanMoney = dto.LoanMoney;//借款金额
                doc.LoanFunctionMoney = dto.LoanMoney;//借款本币金额
                doc.LoanBalance = dto.LoanMoney;//借款余额
                doc.LoanFuntionalBalance = doc.LoanMoney;//借款本币余额

                //实体扩展字段
                if (dto.DescFlexField != null)
                {
                    doc.DescFlexField = new UFIDA.U9.Base.FlexField.DescFlexField.DescFlexSegments();
                    DTOUtils.DTOPropCopy(dto.DescFlexField, doc.DescFlexField);
                }

                //其它
                doc.WorkFlowID = Guid.Empty;
                doc.StateMachineID = Guid.Empty;
                doc.SOB = EntityUtils.getSetofBooks();
                doc.OriginalExchangeRate = 1;
                doc.LoanUse = dto.LoanUse;

                doc.LoanCurrency = EntityUtils.GetEntityByCode<Currency>("C001");
                doc.FunctionalCurrency = doc.LoanCurrency;
                doc.IsCredit = true;
                doc.ExpenseItem = EntityUtils.GetEntityByCode<ExpenseItem>(dto.ExpenseItem);
                if (string.IsNullOrEmpty(dto.ExpectPayDate))
                {
                    doc.ExpectPayDate = doc.LoanDate;
                }
                else
                {
                    doc.ExpectPayDate = DateTime.Parse(dto.ExpectPayDate);
                }

                //银行卡
                //doc.EmployeeBankCardType = EntityUtils.GetEntityByCode<BankCardType>(dto.EmployeeBankCardType);
                doc.EmployeeBankCardNo = EntityUtils.GetEntityByCommon<EmployeeBankAccount>(dto.EmployeeBankCardNo);
                if (doc.EmployeeBankCardNo != null)
                {
                    doc.EmployeeBankCardType = doc.EmployeeBankCardNo.CardType;
                }

                //项目
                doc.Project = EntityUtils.GetEntityByCode<Project>(dto.Project);
                //部门,使用借款组织查询
                doc.Dept = EntityUtils.GetDept(doc.LoanOrg.Code, dto.Dept);

                session.Commit();
            }

            //针对运达项目,清除所有缓存
            U9Utils.ClearCache();
            doc = doc.Key.GetEntity();

            return doc;
        }

        public static LoanBill SubmitDoc(LoanBill doc)
        {
            U9Utils.ClearCache(doc.Key);
            doc = doc.Key.GetEntity();

            if (doc.DocStatus != LoanDocStatusEnum.Opened)
                throw new Exception($"单号[{doc.DocNo}]不为开立,不能提交");

            LoanBillSubmitBP bp = new LoanBillSubmitBP();
            bp.LoanBillID = doc.Key;
            bp.UISysVersion = doc.SysVersion;
            bp.Do();
            return doc;
        }

        public static LoanBill ApproveDoc(LoanBill doc)
        {
            U9Utils.ClearCache(doc.Key);
            doc = doc.Key.GetEntity();

            if (doc.DocStatus != LoanDocStatusEnum.Approving)
                throw new Exception($"单号[{doc.DocNo}]不为提交,不能审核");

            LoanBillApproveBP bp = new LoanBillApproveBP();
            bp.LoanBillID = doc.Key;
            bp.UISysVersion = doc.SysVersion;
            bp.IsApprove = true;
            bp.IsFromMER = false;
            bp.Do();
            return doc;
        }

        public static LoanBill UnApproveDoc(LoanBill doc)
        {
            U9Utils.ClearCache(doc.Key);
            doc = doc.Key.GetEntity();

            if (doc.DocStatus != LoanDocStatusEnum.Approved)
                throw new Exception($"单号[{doc.DocNo}]不为已审,不能弃审");

            LoanBillApproveBP bp = new LoanBillApproveBP();
            bp.LoanBillID = doc.Key;
            bp.UISysVersion = doc.SysVersion;
            bp.IsApprove = false;
            bp.Do();

            return doc;
        }
    }
}
